﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Textbox
{
    /// <summary>
    /// Interaction logic for TextEditor.xaml
    /// </summary>
    public partial class TextEditor : UserControl
    {
        public TextEditor()
        {
            InitializeComponent();
            _textchangedcount = 0;
        }
        public string Text
        {
            get
            {
                return this.TextBox1.Text;

            }
        }
        public void Openfile(string fileforopening)
        {
            StreamReader u = new StreamReader(fileforopening,true);
            string e = u.ReadToEnd();
            TextBox1.Text = e;
            u.Close();
            u.Dispose();
        }
        public void Savefile(string fileforsaving)
        {
            StreamWriter u = new StreamWriter(fileforsaving);
            u.Write(TextBox1.Text);
            u.Close();
            u.Dispose();
        }

        /// <summary>
        /// Not really necessary, but it does happen to make it easier to use or something...
        /// </summary>
        private int _textchangedcount;
        public event TextChangedEventHandler TextChanged;
        /// <summary>
        /// Has to check whether or not the eventhandler is null. Pretty efficient otherwise
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TextBox1_TextChanged(object sender, TextChangedEventArgs e)
        {
            _textchangedcount++;
            if (TextChanged != null)
            {
                if (_textchangedcount > 40)
                {
                    TextChanged.Invoke(this, e);
                    _textchangedcount = 0;
                }
            }

        }
        public event KeyEventHandler PreviewKeyDownstuff;

        private void UserControl_KeyUp(object sender, KeyEventArgs e)
        {

        }
        /// <summary>
        /// Does not actually make any other changes. Probably not the best idea, but it seems to work fairly well.
        /// </summary>
        public void Newfile()
        {
            this.TextBox1.Text = "";
        }
        public double zoom
        {
            set
            {
                TextBox1.FontSize = value;
            }
            get
            {
                return TextBox1.FontSize;
            }
        }
        private void TextBox1_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            if (Keyboard.Modifiers == ModifierKeys.Control)
                switch (e.Key)
                {
                    case Key.S:
                    case Key.N:
                    case Key.O:
                    case Key.Add:
                    case Key.Subtract:
                        PreviewKeyDownstuff.Invoke(this, e);
                        break;
                    default:
                        return;
                }
        }
    }


}

